// ChooseFriendDialog.h
// Created by Limou on 24-8-31.
// 描述一个用于选择好友组建群聊的对话框

#ifndef FRONTEND_CHOOSEFRIENDDIALOG_HPP
#define FRONTEND_CHOOSEFRIENDDIALOG_HPP

#include <QDialog>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include "../../common/Common.hpp"
#include "../../model/UserModel.hpp"
#include "Container.hpp"
#include "HaveContainer.hpp"
#include "SelectedContainer.hpp"

// >>> class ChooseFriendDialog begin: 描述一个用于选择好友组建群聊的对话框 >>>
namespace chat_system::subassembly::ChooseFriendDialog {
    class ChooseFriendDialog : public QDialog {
    Q_OBJECT
    private:
        HaveContainer* haveContainer_; // 已有好友区域部件
        SelectedContainer* selectedContainer_; // 已选好友区域部件

    public:
        // 构造方法
        explicit ChooseFriendDialog()
        : haveContainer_(new HaveContainer(this))
        , selectedContainer_(new SelectedContainer(this)) {
            this->setElement(); // 设置组件
            this->setElementStyle(); // 设置组件样式
            this->setElementLayout(); // 设置组件布局
            this->setElementSignalSlot(); // 设置信号槽
        }

        // 析构方法
        ~ChooseFriendDialog() override = default;

    private:
        // 设置组件
        void setElement() {
            // 设置本部件
            this->setFixedSize(QSize(750, 550));
            this->setWindowTitle("添加其他好友到群聊");
            QIcon windowIcon(":/window/window_main/logo.png");
            if (windowIcon.isNull()) { common::Tools::getInstance()->getLogger()->warn("ChooseFriendDialog::ChooseFriendDialog(QWidget* parent): windowIcon is null"); }
            this->setWindowIcon(windowIcon);
            this->setAttribute(Qt::WA_DeleteOnClose); // 关闭时自动销毁
            this->move(QCursor::pos());
        }

        // 设置组件样式方法
        void setElementStyle() {
            // 提前定义本部件样式
            QString selfStyleSheet =
                    "QDialog {"
                    "background-color: rgb(255, 255, 255);"
                    "}";
            this->setStyleSheet(selfStyleSheet);
        }

        // 设置组件布局方法
        void setElementLayout() {
            // 设置布局管理器
            auto layout = new QHBoxLayout();
            layout->setSpacing(0);
            layout->setContentsMargins(0, 0, 0, 0);

            layout->addWidget(haveContainer_, 0, Qt::AlignCenter);
            layout->addWidget(selectedContainer_, 0, Qt::AlignCenter);

            this->setLayout(layout);
        }

        // 设置信号槽
        void setElementSignalSlot() {
            connect(haveContainer_->getContainer(), &Container::confirm, this, [this](ContainerItem* containerItem) {
                common::Tools::getInstance()->getLogger()->trace("-> {0}", std::to_string(reinterpret_cast<uintptr_t>(containerItem)));
                selectedContainer_->getContainer()->moveItem(containerItem); // 应该会被转移
            });

            connect(haveContainer_->getContainer(), &Container::cancel, this, [this](ContainerItem* containerItem) {
                common::Tools::getInstance()->getLogger()->trace("-< {0}", std::to_string(reinterpret_cast<uintptr_t>(containerItem)));
                haveContainer_->getContainer()->moveItem(containerItem);
            });
        }
    };
} // namespace chat_system::subassembly end
// <<< class ChooseFriendDialog end <<<

#endif //FRONTEND_CHOOSEFRIENDDIALOG_HPP
